package com.itheima.sys.mapper;

import com.itheima.sys.entity.CompanyUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 企业与用户关联表 企业-企业与用户关联表 Mapper 接口
 * </p>
 *
 * @author itheima
 * @since 2020-12-13
 */
public interface CompanyUserMapper extends BaseMapper<CompanyUser> {

    @Select("SELECT DISTINCT\n" +
            "\tcu.*\n" +
            "FROM\n" +
            "\tcompany_user cu\n" +
            "INNER JOIN company_user_role cur ON cur.company_user_id = cu.id\n" +
            "INNER JOIN sys_role r ON cur.role_id = r.id\n" +
            "WHERE\n" +
            "\tcu.company_id = #{companyId}\n" +
            "AND r.role_name LIKE 'ROLE_ADMIN%'")
    List<CompanyUser> queryCompanyAdmins(@Param("companyId") Long companyId);

    @Select("SELECT\n" +
            "\tcu.*\n" +
            "FROM\n" +
            "\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\t*\n" +
            "\t\tFROM\n" +
            "\t\t\tcompany_user\n" +
            "\t\tWHERE\n" +
            "\t\t\tcompany_id = #{companyId}\n" +
            "\t) cu\n" +
            "INNER JOIN company_user_role cur ON cur.company_id = cu.company_id\n" +
            "AND cur.company_user_id = cu.id\n" +
            "INNER JOIN sys_role sr ON cur.role_id = sr.id\n" +
            "WHERE\n" +
            "\tsr.role_name = 'ROLE_ADMIN_SYS'")
    CompanyUser getCurrentAdmin(@Param("companyId") Long companyId);
}
